package com.topisv.tms.repository.notice;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.notice.Notice;

public interface NoticeDao extends PagingAndSortingRepository<Notice, Long>, JpaSpecificationExecutor<Notice> {
    @Query("select f from Notice f where f.status = ?")
    List<Notice> findByStatus(String status);
    
    @Query("select f from Notice f where f.id = ?")
    Notice findById(Long id);
    
    /**
     * 更新所有公告未置顶
     */
    @Modifying
    @Query("update Notice t set t.status=0")
    void update();
    
    /**
     * 置顶当前公告
     */
    @Modifying
    @Query("update Notice t set t.status=1 where id =?")
    void update(Long id);
    
    @Query("SELECT ni FROM Notice ni WHERE STATUS = 1 AND EXISTS(SELECT 1 FROM NoticeReader nr WHERE ni.id = nr.noticeId AND nr.jobuuid = ? ) ")
    List<Notice> findByJobuuidNoReaderNotice(String jobuuid);
}
